
## @knitr DESCRIPTIVE_STATS_AND_MAP

rm(list=ls())

#library(foreign)
#library(stringr)
#library(xlsx)

# from \url{https://www.r-bloggers.com/identifying-the-os-from-r/}
get_os <- function(){
  sysinf <- Sys.info()
  if (!is.null(sysinf)){
    os <- sysinf['sysname']
    if (os == 'Darwin')
      os <- "osx"
  } else { ## mystery machine
    os <- .Platform$OS.type
    if (grepl("^darwin", R.version$os))
      os <- "osx"
    if (grepl("linux-gnu", R.version$os))
      os <- "linux"
  }
  tolower(os)
}

get_machine  <- function(){
    sysinf  <- Sys.info()
    if (!is.null(sysinf)){
        my.machine  <- sysinf['nodename']
    }
    tolower(my.machine)
}


if(get_os()=="windows" & get_machine()=="mompracen") {
    path <- setwd('C:/Users/giaco/Documents/MEGAsync/')
} else if (get_os()=="windows" & get_machine()!="mompracen") {
    path <- setwd('C:/Users/grieco/Dropbox/')
} else if (get_machine()=="aramis") {
    path <- setwd('/home/giacomo68/Dropbox/')
} else {
    path <- setwd('/home/giacomo/Dropbox/')
}



library(survival)
library(xtable)

Data.icc <- read.csv('gc-jg-project/kampala/data/data-rome.csv',
                    stringsAsFactors=FALSE)

Data <- read.csv('gc-jg-project/kampala/data/data-kampala.csv',
                 stringsAsFactors=FALSE)


kampala.t1.lie <- Data[Data$country_name=="Liechtenstein" & Data$kampala==1,"kampala.t1"]

num.signatories <- length(table(Data$country_name[Data$kampala==1]))

## + 2 -- Cook Islands is not included
## Palestine to be added; Palestine becomes eligible to ICC membership since it joins UNESCO
## see Christina Davis' book, IO as Discriminatory Clubs
num.icc.members <- length(table(Data.icc$country_name[Data.icc$rome.party==1])) + 2

perc.in.kampala <- round(100*num.signatories/num.icc.members,1)



#######
#######
## map
#######
#######


library(survival)
library(xtable)
library(ggplot2)
library(english)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(countrycode)

Data.kampala <- read.csv('gc-jg-project/kampala/data/data-kampala.csv',
                  stringsAsFactors=FALSE)

Data.rome <- read.csv('gc-jg-project/kampala/data/data-rome.csv',
                 stringsAsFactors=FALSE)
Data.rome$country_name[Data.rome$country_name=="Estawini"] <- "Eswatini"


D.kampala <- aggregate(Data.kampala$kampala~Data.kampala$country_name,FUN=max) 
D.rome <- aggregate(Data.rome$t.rome~Data.rome$country_name,FUN=max) 

colnames(D.kampala) <- c("country_name","kampala")
colnames(D.rome) <- c("country_name","rome")

D.kampala$wb_a3 <- countrycode(D.kampala$country_name, origin="country.name",
                                    destination="wb")

D.rome$wb_a3 <- countrycode(D.rome$country_name, origin="country.name",
                                    destination="wb")

D.data <- merge(D.rome,D.kampala,by=c("country_name","wb_a3"),all.x=TRUE)

D.data$membership <- NULL
D.data$membership[D.data$rome==0] <- "Non-state party"
D.data$membership[D.data$rome==1 & D.data$kampala==0] <- "Rome state party"
D.data$membership[D.data$rome==1 & is.na(D.data$kampala)] <- "Rome state party"
D.data$membership[D.data$kampala==1] <- "Kampala ratifier"
D.data$membership[D.data$country_name=="Burundi"] <- "Former state party"
D.data$membership[D.data$country_name=="Philippines"] <- "Former state party"
D.data$membership <- factor(D.data$membership,
                            levels=c("Kampala ratifier",
                                     "Rome state party",
                                     "Non-state party",
                                     "Former state party"))


DD.data <- D.data[,c("wb_a3","membership")]


World <- ne_countries(scale = "medium", returnclass = "sf")

World$wb_a3[World$name=="Norway"] <- "NOR"
World$wb_a3[World$name=="Palestine"] <- "PSE"
World$wb_a3[World$name=="Romania"] <- "ROU"
World$wb_a3[World$name=="Dem. Rep. Congo"] <- "COD"
World$wb_a3[World$name=="Andorra"] <- "AND"
World$wb_a3[World$name=="Timor-Leste"] <- "TLS"


World.plus <- merge(World,DD.data,by="wb_a3",all.x=TRUE)

World.plus$membership[World.plus$name=="Palestine"] <- "Kampala ratifier"
World.plus$membership[World.plus$name=="Switzerland"] <- "Non-state party"
World.plus$membership[World.plus$name=="Greenland"] <- "Rome state party"
World.plus$membership[World.plus$name=="Faeroe Is."] <- "Rome state party"
World.plus$membership[is.na(World.plus$membership)] <- "Non-state party"

cbbPalette <- c("#000000", "#56B4E9", "#E69F00", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

## palette2 <- c("#009E73", "#F0E442", "#0072B2")

## knitr MAP_FIGURE
map.plot <- ggplot(data = World.plus) +
    geom_sf(aes(fill = membership)) +
    scale_fill_manual(values=cbbPalette) +
    theme_bw() +
#    ggtitle("") +
    theme(legend.text=element_text(size=15),
          legend.key.size=unit(.3, "in"),
          legend.key.width=unit(1, "in"),
          legend.title=element_blank(),
          legend.position="bottom",
          plot.title = element_text(lineheight=.8, face="bold",size=40)) +
#    labs(color = "MID track record\n") +
    guides(color=guide_legend(nrow=2,byrow=TRUE,reverse=TRUE))

ggsave("gc-jg-project/kampala/paper/figures/figure1.png", plot = map.plot,
    width = 14, height = 8, device = "png")
